﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CamShared;
using System.Drawing;
using DavuxLib;

namespace TimestampFilter
{
    class LagMeter : ICameraFilter
    {

        Dictionary<ICamera, int> Cams = new Dictionary<ICamera, int>();
        Dictionary<ICamera, int> CamLoc = new Dictionary<ICamera, int>();


        public System.Drawing.Image FrameReceived(ICamera camera, System.Drawing.Image frame)
        {


            if (!Cams.ContainsKey(camera))
            {
                if (Settings.Get(camera.Name + "|Display Lag Meter", true))
                {
                    Cams.Add(camera, 0);
                    CamLoc.Add(camera, 0);
                }
                else
                {
                    Cams.Add(camera, -1);
                }
            }
            if (Cams[camera] != -1)
            {
                using (Graphics g = Graphics.FromImage(frame))
                {
                    int val = Cams[camera];
                    //g.FillRectangle(Brushes.MediumPurple, new Rectangle((frame.Width / 40 * val) - 2, frame.Height - 17, 14, 14));
                    //g.FillRectangle(Brushes.Lime, new Rectangle((frame.Width / 40 * val), frame.Height - 15, 10, 10));

                    Image img = null;

                    switch (val)
                    {
                        case 1:
                            img = CamShared.Properties.Resources.spinner_1;
                            break;

                        case 2:
                            img = CamShared.Properties.Resources.spinner_3;
                            break;
                        case 3:
                            img = CamShared.Properties.Resources.spinner_5;
                            break;
                        case 4:
                            img = CamShared.Properties.Resources.spinner_2;
                            break;
                        case 5:
                            img = CamShared.Properties.Resources.spinner_4;
                            break;
                        case 6:
                            img = CamShared.Properties.Resources.spinner_6;
                            break;
                        default:
                            img = CamShared.Properties.Resources.spinner_6;
                            break;
                    }

                    int loc = CamLoc[camera];

                    if (loc > frame.Width - 28)
                    {
                        loc = 0;
                    }


                    g.DrawImage(img, new Rectangle(loc, frame.Height - 26 + 2, 28, 28));
                    Cams[camera] = val >= 6 ? 1 : val += 1;

                    //ac.

                    loc += frame.Width / 20;

                    CamLoc[camera] = loc;

                }
            }
            return frame;
        }

        private Bitmap rotateImage(Bitmap b, float angle)
        {
            //create a new empty bitmap to hold rotated image
            Bitmap returnBitmap = new Bitmap(b.Width, b.Height);
            //make a graphics object from the empty bitmap
            Graphics g = Graphics.FromImage(returnBitmap);
            //move rotation point to center of image
            g.TranslateTransform((float)b.Width / 2, (float)b.Height / 2);
            //rotate
            g.RotateTransform(angle);
            //move image back
            g.TranslateTransform(-(float)b.Width / 2, -(float)b.Height / 2);
            //draw passed in image onto graphics object
            g.DrawImage(b, new Point(0, 0));
            return returnBitmap;
        }

        public event CamShared.CamShared.RaiseRame OnNewFrame;

        public string Name
        {
            get { return "Lag Meter"; }
        }



        public void Dispose()
        {

        }

    }
}
